<template>
  <div class="layout-container">
    <!-- 顶部导航栏 -->
    <header class="layout-header">
      <nav class="navbar" :class="{ 'navbar-transparent': isHomePage }">
        <div class="nav-container">
          <div class="nav-brand">
            <router-link to="/" class="logo-link">
              <span class="logo-text">模拟面试</span>
            </router-link>
          </div>
          <div class="nav-menu">
            <router-link to="/" class="nav-link">首页</router-link>
            <router-link to="/interview-questions" class="nav-link">题目库</router-link>
            <a href="#" class="nav-link">关于</a>
            <a href="#" class="nav-link">帮助</a>
            <!-- 用户信息 -->
            <el-dropdown v-if="authStore.isAuthenticated" trigger="click" @command="handleUserCommand">
              <div class="user-info-el">
                <el-avatar :size="32" icon="UserFilled" />
                <span class="username">{{ authStore.userInfo?.username || '用户' }}</span>
                <el-icon class="dropdown-icon">
                  <arrow-down />
                </el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">
                    <el-icon><User /></el-icon>
                    个人中心
                  </el-dropdown-item>
                  <el-dropdown-item command="settings">
                    <el-icon><Setting /></el-icon>
                    设置
                  </el-dropdown-item>
                  <el-dropdown-item divided command="logout">
                    <el-icon><switch-button /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <router-link v-else to="/login" class="nav-link login-link">登录</router-link>
          </div>
        </div>
      </nav>
    </header>

    <!-- 主要内容区域 -->
    <main class="layout-main">
      <slot></slot>
    </main>

    <!-- 底部栏 -->
    <footer class="layout-footer" :class="{ 'footer-transparent': isHomePage }">
      <div class="footer-container">
        <div class="footer-content">
          <p>&copy; 2025 模拟面试. All rights reserved.</p>
        </div>
      </div>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { useRoute, useRouter } from 'vue-router'
import { computed, onMounted } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { ArrowDown, SwitchButton, User, Setting } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

const isHomePage = computed(() => route.path === '/')

const handleLogout = async () => {
  try {
    await authStore.logout()
    router.push('/login')
  } catch (error) {
    console.error('退出登录失败:', error)
  }
}

const handleUserCommand = (command: string) => {
  switch (command) {
    case 'profile':
      // 跳转到个人中心页面，这里假设路由为 /profile
      router.push('/profile')
      break
    case 'settings':
      // 跳转到设置页面，这里假设路由为 /settings
      router.push('/settings')
      break
    case 'logout':
      handleLogout()
      break
  }
}

onMounted(() => {
  // 如果已登录，获取用户信息
  try {
    if (authStore.isAuthenticated && !authStore.userInfo) {
      authStore.fetchUserInfo().catch(err => {
        console.error('获取用户信息失败:', err)
      })
    }
  } catch (error) {
    console.error('初始化用户信息失败:', error)
  }
})
</script>

<style scoped>
.layout-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.layout-header {
  flex-shrink: 0;
  position: relative;
  z-index: 10;
}

.layout-main {
  flex: 1;
  min-height: 0;
  position: relative;
  z-index: 1;
}

.layout-footer {
  flex-shrink: 0;
  margin-top: auto;
  position: relative;
  z-index: 10;
}

/* 导航栏样式 */
.navbar {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 1rem 2rem;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  border-bottom: 1px solid #e5e5e5;
  transition: all 0.3s ease;
}

.navbar-transparent {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-bottom: none;
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
}

.navbar-transparent .logo-text {
  color: white;
}

.navbar-transparent .nav-link {
  color: white;
}

.navbar-transparent .nav-link:hover {
  color: rgba(255, 255, 255, 0.8);
}

.navbar-transparent .nav-link::after {
  background: white;
}

.navbar-transparent .nav-link.router-link-active {
  color: white;
}

.nav-container {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nav-brand {
  display: flex;
  align-items: center;
}

.logo-link {
  text-decoration: none;
  color: inherit;
}

.logo-text {
  font-size: 1.5rem;
  font-weight: bold;
  color: #333;
  transition: color 0.3s ease;
}

.logo-link:hover .logo-text {
  color: #667eea;
}

.nav-menu {
  display: flex;
  align-items: center;
  gap: 2rem;
}

.nav-link {
  color: #333;
  text-decoration: none;
  font-size: 1rem;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
  padding: 0.5rem 0;
}

.nav-link:hover {
  color: #667eea;
}

.nav-link.router-link-active {
  color: #667eea;
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background: #667eea;
  transition: width 0.3s ease;
}

.nav-link:hover::after,
.nav-link.router-link-active::after {
  width: 100%;
}

/* 用户菜单样式 */
.user-info-el {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: rgba(102, 126, 234, 0.1);
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #667eea;
  font-weight: 500;
}

.navbar-transparent .user-info-el {
  background: rgba(255, 255, 255, 0.2);
  color: white;
}

.user-info-el:hover {
  background: rgba(102, 126, 234, 0.2);
}

.navbar-transparent .user-info-el:hover {
  background: rgba(255, 255, 255, 0.3);
}

.username {
  font-size: 0.9rem;
}

.dropdown-icon {
  transition: transform 0.3s ease;
  color: currentColor;
}

.login-link {
  margin-left: 1rem;
  padding: 0.5rem 1rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white !important;
  border-radius: 20px;
  transition: all 0.3s ease;
}

.login-link:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.login-link::after {
  display: none;
}

.navbar-transparent .login-link {
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
}

.navbar-transparent .login-link:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* 底部栏样式 */
.layout-footer {
  background: #2d3748;
  color: #e2e8f0;
  padding: 2rem 2rem;
  transition: all 0.3s ease;
}

.footer-transparent {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.footer-transparent .footer-content p {
  color: white;
}

.footer-container {
  max-width: 1400px;
  margin: 0 auto;
}

.footer-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

.footer-content p {
  margin: 0;
  font-size: 0.875rem;
  opacity: 0.9;
}

.footer-links {
  display: flex;
  gap: 1.5rem;
  flex-wrap: wrap;
}

.footer-link {
  color: #e2e8f0;
  text-decoration: none;
  font-size: 0.875rem;
  transition: color 0.3s ease;
}

.footer-link:hover {
  color: #667eea;
}

.footer-link.router-link-active {
  color: #667eea;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .navbar {
    padding: 1rem;
  }

  .nav-container {
    flex-direction: column;
    gap: 1rem;
  }

  .nav-menu {
    gap: 1rem;
    flex-wrap: wrap;
    justify-content: center;
  }

  .footer-content {
    text-align: center;
  }
}
</style>